<?php
$db=&JFactory::getDbo();
$user=&JFactory::getUser();
//echo date("d.m.Y H:i:s");
if(JRequest::getVar("d","")==""){
	$html= '<h1>Sıralama Kontrolü Yapılan Domainler Tarihlere Göre Ortalama Sıralar</h1>';
	if($user->id=="309")
	$sql="SELECT domain FROM izlenecekdomainler group by domain ";
	else
	$sql="SELECT domain FROM izlenecekdomainler where userid='".$user->id."' group by domain ";
	$db->setQuery($sql);
	$rows=$db->loadObjectList();
	$adet=count($rows);
	$page=JRequest::getVar("page","1");
	$count=JRequest::getVar("count","25");
	$pagecount=ceil($adet/$count);
	$url=explode("?", $_SERVER["REQUEST_URI"]);
	if($user->id=="309")
	$sql="SELECT domain FROM izlenecekdomainler group by domain limit ".($page-1)*$count.",".$count;
	else
	$sql="SELECT domain FROM izlenecekdomainler where userid='".$user->id."' group by domain limit ".($page-1)*$count.",".$count;
	
	for($i=0;$i<$pagecount;$i++){
		if($i==$page-1) $class="primary"; else $class="default";
		$bar[]='<a href="'.$url[0]."?count=".$count."&page=".($i+1).'" class="button-'.$class.'">'.($i+1).'</a>';
	}
	$b=implode("", $bar);
	$db->setQuery($sql);
	$rows=$db->loadObjectList();
	$html.= '<div style="box"align="right"><a href="/googleadddomain.html" class="button-primary">Yeni Domain ekle</a></div>';
	$html.= '<table class="zebra">';
	$days=0;
	$html.= '<tr>'.'<td>Domainler</td>';
	for($days=6;$days>=0;$days--){
		$day=date("d M",strtotime("-".$days." day"));
		$html.='<td>'.$day.'</td>';
	}
	$html.='</tr>';

if(1==1){
			$day6=date("Y-m-d",strtotime("-6day"));
			$day5=date("Y-m-d",strtotime("-5day"));
			$day4=date("Y-m-d",strtotime("-4day"));
			$day3=date("Y-m-d",strtotime("-3day"));
			$day2=date("Y-m-d",strtotime("-2day"));
			$day1=date("Y-m-d",strtotime("-1day"));
			$day0=date("Y-m-d");
			if($user->id!="309") $where="having userid='".$user->id."'";else $where="";
			if($user->id!="309") $where2="where userid='".$user->id."'";else $where2="";
			$sql="select domain,userid
, avg(tb1.day6) as day6
, avg(tb1.day5) as day5
, avg(tb1.day4) as day4
, avg(tb1.day3) as day3
, avg(tb1.day2) as day2
, avg(tb1.day1) as day1
, avg(tb1.day0) as day0

from (SELECT domain,kelimeler ,userid,
(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and tarihsaat>'".$day6." 00:00:00' and tarihsaat<'".$day6." 23:59:59' limit 0,1 )as day6 ,
(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and tarihsaat>'".$day5." 00:00:00' and tarihsaat<'".$day5." 23:59:59' limit 0,1 )as day5 ,
(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and tarihsaat>'".$day4." 00:00:00' and tarihsaat<'".$day4." 23:59:59' limit 0,1 )as day4 ,
(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and tarihsaat>'".$day3." 00:00:00' and tarihsaat<'".$day3." 23:59:59' limit 0,1 )as day3 ,
(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and tarihsaat>'".$day2." 00:00:00' and tarihsaat<'".$day2." 23:59:59' limit 0,1 )as day2 ,
(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and tarihsaat>'".$day1." 00:00:00' and tarihsaat<'".$day1." 23:59:59' limit 0,1 )as day1 ,
(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and tarihsaat>'".$day0." 00:00:00' and tarihsaat<'".$day0." 23:59:59' limit 0,1 )as day0 
FROM `izlenecekdomainler` ".$where2." ) as tb1 group by  domain ".$where."  limit ".($page-1)*$count.",".$count;
			//echo $sql;
		if($user-id==309) echo $sql;	
			$db->setQuery($sql);
			$siralama=$db->loadObjectList();
		$eskisira=0;
		foreach ($siralama as $s){
			$ort=round($siralama[0]->sira);
			if($s->day6<$s->day5)$d5="down";elseif ($s->day6==$s->day5)$d5="";else$d5="up";
			if($s->day5<$s->day4)$d4="down";elseif ($s->day5==$s->day4)$d4="";else$d4="up";
			if($s->day4<$s->day3)$d3="down";elseif ($s->day4==$s->day3)$d3="";else$d3="up";
			if($s->day3<$s->day2)$d2="down";elseif ($s->day3==$s->day2)$d2="";else$d2="up";
			if($s->day2<$s->day1)$d1="down";elseif ($s->day2==$s->day1)$d1="";else$d1="up";
			if($s->day1<$s->day0)$d0="down";elseif ($s->day1==$s->day0)$d0="";else$d0="up";
			if($s->day6==0)$s->day6="-";else $s->day6=round($s->day6);
			if($s->day5==0)$s->day5="-";else $s->day5=round($s->day5);
			if($s->day4==0)$s->day4="-";else $s->day4=round($s->day4);
			if($s->day3==0)$s->day3="-";else $s->day3=round($s->day3);
			if($s->day2==0)$s->day2="-";else $s->day2=round($s->day2);
			if($s->day1==0)$s->day1="-";else $s->day1=round($s->day1);
			if($s->day0==0)$s->day0="-";else $s->day0=round($s->day0);
			$html.= '<tr><td><a href="/tr/googlerankspage.html?d='.$s->domain.'">'.$s->domain.'</a></td>
			<td>'.$s->day6.'</td>
			<td class="'.$d5.'">'.$s->day5.'</td>
			<td class="'.$d4.'">'.$s->day4.'</td>
			<td class="'.$d3.'">'.$s->day3.'</td>
			<td class="'.$d2.'">'.$s->day2.'</td>
			<td class="'.$d1.'">'.$s->day1.'</td>
			<td class="'.$d0.'">'.$s->day0.'</td></tr>';
		}
		}
	$html.= "</table>";
	$html.= '<div align="center" class="box-content">'.$b.'</a>';
}else{
	if($user->id=="309")
	$sql="SELECT * FROM izlenecekdomainler where domain='".JRequest::getVar("d")."'";
	else
	$sql="SELECT * FROM izlenecekdomainler where domain='".JRequest::getVar("d")."' and userid='".$user->id."'";
	$db->setQuery($sql);
	$rs=$db->loadObjectList();
	if(JRequest::getvar("type")!="pdf")
	$html= '<div style="box"align="right"><a href="/googleadddomain.html?domainname='.JRequest::getVar("d").'&task=adddomain" class="button-primary">Yeni Kelime Ekle</a></div>';
	else $html="";
	$html.= '<table class="zebra">';
	$days=0;
	$html.='<tr><td colspan="8"><b>Domain: <b>'.JRequest::getVar("d").'</b></td></tr>';
	$html.= '<tr>'.'<td>Kelimeler</td>';
	for($days=6;$days>=0;$days--){
		$day=date("d M",strtotime("-".$days." day"));
		$html.='<td>'.$day.'</td>';
	}
	$html.= '</tr>';
	if(1==1){
		$day6=date("Y-m-d",strtotime("-6day"));
		$day5=date("Y-m-d",strtotime("-5day"));
		$day4=date("Y-m-d",strtotime("-4day"));
		$day3=date("Y-m-d",strtotime("-3day"));
		$day2=date("Y-m-d",strtotime("-2day"));
		$day1=date("Y-m-d",strtotime("-1day"));
		$day0=date("Y-m-d");

		$sql="select * from (SELECT domain,kelimeler
	,(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and 
tarihsaat>'".$day6." 00:00:00' and tarihsaat<'".$day6." 23:59:59' limit 0,1 )as day6
,(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and 
tarihsaat>'".$day5." 00:00:00' and tarihsaat<'".$day5." 23:59:59' limit 0,1 )as day5
,(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and 
tarihsaat>'".$day4." 00:00:00' and tarihsaat<'".$day4." 23:59:59' limit 0,1 )as day4
	,(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and 
tarihsaat>'".$day3." 00:00:00' and tarihsaat<'".$day3." 23:59:59' limit 0,1 )as day3
,(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and 
tarihsaat>'".$day2." 00:00:00' and tarihsaat<'".$day2." 23:59:59' limit 0,1 )as day2
,(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and 
tarihsaat>'".$day1." 00:00:00' and tarihsaat<'".$day1." 23:59:59' limit 0,1 )as day1
,(select sira from googleresultpage where `query`=`izlenecekdomainler`.kelimeler and domain=`izlenecekdomainler`.domain and 
tarihsaat>'".$day0." 00:00:00' and tarihsaat<'".$day0." 23:59:59' limit 0,1 )as day0
FROM `izlenecekdomainler` 
where domain='".JRequest::getVar("d")."') as t1 order by day0 asc";
		$db->setQuery($sql);
		$siralama=$db->loadObjectList();
		$eskisira=0;
		foreach ($siralama as $s){
			

			if($s->day6<$s->day5)$d5="down";elseif ($s->day6==$s->day5)$d5="";else$d5="up";
			if($s->day5<$s->day4)$d4="down";elseif ($s->day5==$s->day4)$d4="";else$d4="up";
			if($s->day4<$s->day3)$d3="down";elseif ($s->day4==$s->day3)$d3="";else$d3="up";
			if($s->day3<$s->day2)$d2="down";elseif ($s->day3==$s->day2)$d2="";else$d2="up";
			if($s->day2<$s->day1)$d1="down";elseif ($s->day2==$s->day1)$d1="";else$d1="up";
			if($s->day1<$s->day0)$d0="down";elseif ($s->day1==$s->day0)$d0="";else$d0="up";
			if($s->day6=="")$s->day6="-";else $s->day6=round($s->day6);
			if($s->day5=="")$s->day5="-";else $s->day5=round($s->day5);
			if($s->day4=="")$s->day4="-";else $s->day4=round($s->day4);
			if($s->day3=="")$s->day3="-";else $s->day3=round($s->day3);
			if($s->day2=="")$s->day2="-";else $s->day2=round($s->day2);
			if($s->day1=="")$s->day1="-";else $s->day1=round($s->day1);
			if($s->day0=="")$s->day0="-";else $s->day0=round($s->day0);
			$html.= '<tr><td>'.$s->kelimeler.'</td>
			<td>'.$s->day6.'</td>
			<td class="'.$d5.'">'.$s->day5.'</td>
			<td class="'.$d4.'">'.$s->day4.'</td>
			<td class="'.$d3.'">'.$s->day3.'</td>
			<td class="'.$d2.'">'.$s->day2.'</td>
			<td class="'.$d1.'">'.$s->day1.'</td>
			<td class="'.$d0.'">'.$s->day0.'</td></tr>';
		}
	}
	$html.='</table>';

$doc=&JFactory::getDocument();
if(JRequest::getvar("type")=="pdf"){
	$html.='<link rel="stylesheet" href="https://www.seowarp.net/cache/template/widgetkit-4b2872a6-ce167304.css" type="text/css" />
<link rel="stylesheet" href="https://www.seowarp.net/cache/template/template-fb6ea3e3.css" /> ';
	$html='<style>'.$css.'</style>'.$html;
	
	//define("FPDF_FONTPATH", $_SERVER["DOCUMENT_ROOT"]."/pdf/font/");
jimport("pdf.pdf");
	$pdf = new HTML2PDF();
// First page
 $html2pdf = new HTML2PDF('P', 'A4', 'tr');
        $html2pdf->writeHTML($html, true);
        $html2pdf->Output();
exit();
}else {echo $html;

$doc->addStyleDeclaration($css);
